package com.winse.view;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

import com.winse.java.converter.impl.JavaSourceConverter;

public class JavaViewerActivity extends Activity {

	TextView sourceViewer = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		sourceViewer = (TextView) findViewById(R.id.source);
		
		/** solution1 */
//		String html = Java2Html.convertToHtml(/*getString(R.string.java_source)*/getSourceString());
//		sourceViewer.setText(Html.fromHtml(html));

		/** solution2 */
		try {
			sourceViewer.setText(JavaSourceConverter.convert(getSourceStream()));
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
	}
	
	/*
	 * private static JavaSource doParse(String source) throws IOException {
	 * 		StringReader stringReader = new StringReader(source); 
	 * 		JavaSourceParser parser = new JavaSourceParser(); 
	 * 		return parser.parse(stringReader); 
	 * }
	 */

	private InputStream getSourceStream() {
		return getResources().openRawResource(R.raw.javasource);
	}
	
	private String getSourceString(){
		InputStream input = getSourceStream();
		
		BufferedReader reader = new BufferedReader(new InputStreamReader(input));
		StringBuilder builder = new StringBuilder();

		String temp = null;
		try {
			while ((temp = reader.readLine()) != null) {
				builder.append(temp);
				builder.append("\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return builder.toString();
	}
}